package com.cal.calonecal.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cal.calonecal.common.Response;
import com.cal.calonecal.mapper.CompleteEquationMapper;
import com.cal.calonecal.mapper.EquationMapper;
import com.cal.calonecal.pojo.entity.CompleteEquationEntity;
import com.cal.calonecal.pojo.entity.EquationEntity;
import com.cal.calonecal.pojo.entity.UserInfo;
import com.cal.calonecal.pojo.vo.EquationVO;
import com.cal.calonecal.pojo.vo.ProblemLog;
import com.cal.calonecal.pojo.vo.WrongEquationVO;
import com.cal.calonecal.service.IExamService;
import com.cal.calonecal.service.ICompleteEquationService;
import com.cal.calonecal.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 76004
 * @apiNote
 * @date 2023/5/29 0:11
 */
@Service
public class WrongEquationServiceImpl extends ServiceImpl<CompleteEquationMapper, CompleteEquationEntity>implements ICompleteEquationService {
    @Autowired
    private UserUtil userUtil;
    @Autowired
    private IExamService examService;
    @Autowired
    private CompleteEquationMapper completeEquationMapper;
    @Autowired
    private EquationMapper equationMapper;
    @Override
    public Response<List<WrongEquationVO>> selectPage(Integer pageNum, Integer pageSize) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        return Response.success(page(new Page<CompleteEquationEntity>(pageNum, pageSize), new QueryWrapper<CompleteEquationEntity>()
                .eq("uid", userUtil.getUserId(request))
                .eq("is_correct",0)).getRecords()
                .stream().map(wrongEquationEntity -> {
            WrongEquationVO wrongEquationVO =  new WrongEquationVO();
            EquationEntity equation = equationMapper.selectById(wrongEquationEntity.getEquationId());
            wrongEquationVO = (BeanUtil.copyProperties(equation, WrongEquationVO.class,"id"));
            wrongEquationVO.setId(wrongEquationEntity.getId());
                    System.out.println(wrongEquationEntity.getId());
            return wrongEquationVO;
        }).collect(Collectors.toList()));

    }

    @Override
    public Response<List<EquationVO>> redo(Integer num) {
        return Response.success(completeEquationMapper.selectByRand(num, userUtil.getUserId()));
    }

    @Override
    public Response<ProblemLog> getProblemLog() {
        UserInfo userInfo = userUtil.getUser();
        Integer weekNum = completeEquationMapper.selectCount(new QueryWrapper<CompleteEquationEntity>().eq("uid", userInfo.getId())
                .ge("create_time", new Date(System.currentTimeMillis() - 7L * 24 * 60 * 60 * 1000)));
        Integer monthNum = completeEquationMapper.selectCount(new QueryWrapper<CompleteEquationEntity>().eq("uid", userInfo.getId())
                .ge("create_time", new Date(System.currentTimeMillis() - 30L * 24 * 60 * 60 * 1000)));
        Integer weekCorrectNum = completeEquationMapper.selectCount(new QueryWrapper<CompleteEquationEntity>().eq("uid", userInfo.getId())
                        .eq("is_correct",1)
                .ge("create_time", new Date(System.currentTimeMillis() - 7L * 24 * 60 * 60 * 1000)));
        Integer monthCorrectNum = completeEquationMapper.selectCount(new QueryWrapper<CompleteEquationEntity>().eq("uid", userInfo.getId())
                .eq("is_correct",1)
                .ge("create_time", new Date(System.currentTimeMillis() - 30L * 24 * 60 * 60 * 1000)));
        Double weekCorrectRate = weekCorrectNum*1.0/(weekNum==0?1:weekNum);
        Double monthCorrectRate = monthCorrectNum*1.0/(monthNum==0?1:monthNum);
        ProblemLog problemLog = new ProblemLog();
        problemLog.setMonthNum(monthNum);
        problemLog.setMonthCorrectNum(monthCorrectNum);
        problemLog.setWeekCorrectNum(weekCorrectNum);
        problemLog.setWeekNum(weekNum);
        problemLog.setMonthCorrectRate(monthCorrectRate);
        problemLog.setWeekCorrectRate(weekCorrectRate);
        return Response.success(problemLog);
    }
}
